<template>
  <div>
    
    <!-- 权限控制的内容区域 -->
    <div v-auth="['admin-home-project']" class="admin-content">
      <contract-stat :stats="contractStats" />
      <contract-trend :months="contractTrendMonths" :series="contractTrendSeries" :legend="contractTrendLegend" />
      <div style="margin: 24px 0 8px 0; font-weight: bold; font-size: 16px;">最近收款记录</div>
      <recent-payment :data="paymentList" />
    </div>
    
    <!-- 普通用户视图 -->
    <div v-auth="['admin-home-contract']" class="user-content">
         <!-- 项目统计卡片 -->
         <project-stat :stats="projectStats" />
        <!-- 统计分析 -->
        <div class="section-title">
          <Icon type="md-analytics" />
          <span>统计分析</span>
        </div>
        <statistics-analysis :data="statisticsData" />
        
        <!-- 最新填报记录 -->
        <div class="section-title">
          <Icon type="md-list" />
          <span>最新填报记录</span>
        </div>
		<recent-work :data="recentWorktimeList" />
    </div>
  </div>
</template>

<script>
import projectStat from './components/project-stat.vue';
import projectTrend from './components/project-trend.vue';
import projectInfoTable from './components/project-info-table.vue';
import worktimeManagement from './components/worktime-management.vue';
import statisticsAnalysis from './components/statistics-analysis.vue';
import contractStat from './components/contract-stat.vue';
import recentPayment from './components/recent-payment.vue';
import recentWork from './components/recent-work.vue';
import contractTrend from './components/contract-trend.vue';
import { auth } from '@/api/system';
import { ListIndexApi } from '@/api/contract';
import { indexDataApi } from '@/api/work';


export default {
  name: 'index',
  components: {
    projectStat,
    projectTrend,
    projectInfoTable,
    worktimeManagement,
    statisticsAnalysis,
	contractStat,
	recentPayment,
	contractTrend,
	recentWork
  },
  data() {
    return {
      isAdmin: false,
      userRole: '',
      projectStats: [
        { title: '项目总数', value: 0, icon: 'md-folder', iconColor: '#2d8cf0' },
        { title: '未开始数量', value: 0, icon: 'md-time', iconColor: '#808695' },
        { title: '进行中数量', value: 0, icon: 'md-sync', iconColor: '#19be6b' },
        { title: '已完成数量', value: 0, icon: 'md-checkmark-circle', iconColor: '#ff9900' },
        { title: '已暂停数量', value: 0, icon: 'md-pause', iconColor: '#ed4014' }
      ],
      projectTrendMonths: [
        '1月', '2月', '3月', '4月', '5月', '6月',
        '7月', '8月', '9月', '10月', '11月', '12月'
      ],
      projectTrendSeries: [
        { name: '2024年', data: [12, 15, 18, 22, 25, 28, 30, 32, 35, 38, 42, 45] },
        { name: '2025年', data: [18, 22, 25, 28, 32, 35, 38, 42, 45, 48, 52, 55] }
      ],
      projectTrendLegend: ['2024年', '2025年'],
      statisticsData: {
        departmentStats: [
          { department: '技术部', totalHours: 1280, avgHours: 32, projectCount: 45 },
          { department: '产品部', totalHours: 960, avgHours: 24, projectCount: 28 },
          { department: '设计部', totalHours: 720, avgHours: 18, projectCount: 22 },
          { department: '测试部', totalHours: 640, avgHours: 16, projectCount: 18 },
          { department: '运营部', totalHours: 480, avgHours: 12, projectCount: 15 }
        ],
        weeklyStats: [
          { week: '第1周', totalHours: 680, submittedCount: 4, pendingCount: 1 },
          { week: '第2周', totalHours: 720, submittedCount: 5, pendingCount: 0 },
          { week: '第3周', totalHours: 640, submittedCount: 4, pendingCount: 1 },
          { week: '第4周', totalHours: 760, submittedCount: 5, pendingCount: 0 }
        ],
        projectProgress: [
          { status: '进行中', count: 89, percentage: 57 },
          { status: '已完成', count: 38, percentage: 24 },
          { status: '未开始', count: 23, percentage: 15 },
          { status: '已暂停', count: 6, percentage: 4 }
        ]
      },
      projectList: [
        { id: 1, name: '智能家居系统开发', status: 1, progress: 75, leader: '张三', department: '技术部', start_time: '2024-01-15', end_time: '2024-06-30' },
        { id: 2, name: '电商平台优化项目', status: 1, progress: 45, leader: '李四', department: '产品部', start_time: '2024-02-01', end_time: '2024-08-15' },
        { id: 3, name: '数据分析系统', status: 2, progress: 100, leader: '王五', department: '技术部', start_time: '2023-10-01', end_time: '2024-03-31' },
        { id: 4, name: '移动端APP开发', status: 0, progress: 0, leader: '赵六', department: '技术部', start_time: '2024-04-01', end_time: '2024-09-30' }
      ],
      worktimeData: [
        { department: '技术部', week: '2024年第1周', hours: 320, status: '已提交' },
        { department: '产品部', week: '2024年第1周', hours: 160, status: '已提交' },
        { department: '设计部', week: '2024年第1周', hours: 120, status: '待提交' },
        { department: '测试部', week: '2024年第1周', hours: 200, status: '已提交' }
      ],
      recentWorktimeList: [
		  
	  ],
	  //管理员
	  visitType: 'day', // day, month, year
	  visitDate: [new Date(), new Date()],
	  force_reminder: null,
	  contractStats: [
	    { title: '合同总数', value: 0, icon: 'md-list-box', iconColor: '#2d8cf0' },
	    { title: '已完成合同', value: 0, icon: 'md-checkmark-circle', iconColor: '#ff9900' },
	    { title: '执行中合同', value: 0, icon: 'md-sync', iconColor: '#19be6b' },
	    { title: '最近收款合同', value: 0, icon: 'md-cash', iconColor: '#ed4014' },
	    { title: '坏帐合同', value: 0, icon: 'md-alert', iconColor: '#808695' }
	  ],
	  paymentList: [
	    { index: 1, contract_name: '合同A', company_name: '甲方A', total_amount: 100000, amount: 20000, pay_date: '2024-06-01', real_name: '张三' },
	    { index: 2, contract_name: '合同B', company_name: '甲方B', total_amount: 80000, amount: 15000, pay_date: '2024-06-02', real_name: '李四' },
	    { index: 3, contract_name: '合同C', company_name: '甲方C', total_amount: 120000, amount: 30000, pay_date: '2024-06-03', real_name: '王五' }
	  ],
	  contractTrendMonths: [
	    '1月', '2月', '3月', '4月', '5月', '6月',
	    '7月', '8月', '9月', '10月', '11月', '12月'
	  ],
	  contractTrendSeries: [
	    // { name: '2024年', data: [8, 10, 12, 15, 18, 20, 22, 25, 28, 30, 32, 35] },
	    // { name: '2025年', data: [12, 15, 18, 20, 22, 25, 30, 28, 26, 32, 35, 40] }
	  ],
	  contractTrendLegend: [],
    };
  },
  mounted() {
    this.getAuth();
    this.checkUserRole();
	this.getData();
	this.getDataContract();
  },
  methods: {
	getData() {
		let that = this;
		ListIndexApi()
		  .then((res) => {
			  let data = res.data || {};
		     that.contractTrendSeries = data.year.data;
			 that.contractTrendLegend = data.year.years;
			 that.paymentList = data.pay;
			 console.info(that.projectTrendSeries)
			 
			 that.contractStats =[
			   { title: '合同总数', value: data.count.total_count, icon: 'md-list-box', iconColor: '#2d8cf0' },
			   { title: '已完成合同', value: data.count.completed_count, icon: 'md-checkmark-circle', iconColor: '#ff9900' },
			   { title: '执行中合同', value: data.count.executing_count, icon: 'md-sync', iconColor: '#19be6b' },
			   { title: '最近收款合同', value: data.count.recent_payment_count, icon: 'md-cash', iconColor: '#ed4014' },
			   { title: '坏帐合同', value: data.count.bad_count, icon: 'md-alert', iconColor: '#808695' }
			 ];
		  })
		  .catch((res) => {});
	},
	
	getDataContract() {
		let that = this;
		indexDataApi()
		  .then((res) => {
			  let data = res.data || {};
			  that.recentWorktimeList = data.records;
			  that.statisticsData.departmentStats = data.dept_data;
			  that.statisticsData.weeklyStats = data.week_data;
		      that.projectStats = [
		        { title: '项目总数', value: data.total.total_count, icon: 'md-folder', iconColor: '#2d8cf0' },
		        { title: '未开始数量', value: data.total.not_started_count, icon: 'md-time', iconColor: '#808695' },
		        { title: '进行中数量', value: data.total.in_progress_count, icon: 'md-sync', iconColor: '#19be6b' },
		        { title: '已完成数量', value: data.total.completed_count, icon: 'md-checkmark-circle', iconColor: '#ff9900' },
		        { title: '已暂停数量', value: data.total.paused_count, icon: 'md-pause', iconColor: '#ed4014' }
		      ];
		  })
		  .catch((res) => {});
	},
    getAuth() {
      auth()
        .then((res) => {
          let data = res.data || {};
          this.force_reminder = data.force_reminder;
          if (data.auth_code && data.auth) {
            this.authCode = data.auth_code;
            this.auth = true;
          }
        })
        .catch((res) => {});
    },
    checkUserRole() {
      // 模拟权限检查，实际项目中应该从用户信息或token中获取
      const userInfo = this.$store.state.user;
      this.userRole = userInfo ? userInfo.role : 'user';
      this.isAdmin = this.userRole === 'admin' || this.userRole === 'manager';
    },
    loadRecentWorktime() {
      // 加载最新填报记录
      // 实际项目中应该调用API
      console.log('加载最新填报记录');
    }
  },
};
</script>

<style lang="less" scoped>
.section-title {
  display: flex;
  align-items: center;
  margin: 24px 0 16px 0;
  font-weight: bold;
  font-size: 16px;
  color: #333;
  
  i {
    margin-right: 8px;
    color: #2d8cf0;
  }
}

.admin-content,
.user-content {
  margin-top: 24px;
}
</style>
